package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.system.ScenicOperationDynamicInfo;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.service.IScenicOperationDynamicInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.List;

/**
 * 运营动态信息。Controller
 *
 * @author zjw
 * @date 2023-09-12
 */
@RestController
@RequestMapping("/system/scenicOperationDynamicInfo")
public class ScenicOperationDynamicInfoController extends BaseController {
    @Autowired
    private IScenicOperationDynamicInfoService operationInfoService;

    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * 分页查询运营动态信息列表
     */
//    @PreAuthorize("@ss.hasPermi('system:info:list')")
    @GetMapping("/listByPage")
    public TableDataInfo selectScenicOperationDynamicInfoListByPage(ScenicOperationDynamicInfo scenicOperationDynamicInfo) {
        startPage();
        List<ScenicOperationDynamicInfo> list = operationInfoService.selectOperationInfoList(scenicOperationDynamicInfo);
        return getDataTable(list);
    }

    @GetMapping("/list")
    public AjaxResult list(ScenicOperationDynamicInfo scenicOperationDynamicInfo) {
        List<ScenicOperationDynamicInfo> list = operationInfoService.selectOperationInfoList(scenicOperationDynamicInfo);

        //将所有对象按照创建时间倒序排列
        Collections.sort(list, (o1, o2) -> {
            if (o1.getCreateTime().getTime() >= o2.getCreateTime().getTime()) {
                return -1;
            } else {
                return 1;
            }
        });
        return success(list);
    }

    /**
     * 导出运营动态信息。列表
     */
    @PreAuthorize("@ss.hasPermi('system:info:export')")
    @Log(title = "运营动态信息。", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ScenicOperationDynamicInfo scenicOperationDynamicInfo) {
        List<ScenicOperationDynamicInfo> list = operationInfoService.selectOperationInfoList(scenicOperationDynamicInfo);
        ExcelUtil<ScenicOperationDynamicInfo> util = new ExcelUtil<ScenicOperationDynamicInfo>(ScenicOperationDynamicInfo.class);
        util.exportExcel(response, list, "运营动态信息。数据");
    }

    /**
     * 获取运营动态信息。详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:info:query')")
    @GetMapping(value = "/{operationInfoId}")
    public AjaxResult getInfo(@PathVariable("operationInfoId") Long operationInfoId) {
        return success(operationInfoService.selectOperationInfoByOperationInfoId(operationInfoId));
    }

    /**
     * 新增运营动态信息。
     */
    @PreAuthorize("@ss.hasPermi('system:info:add')")
    @Log(title = "运营动态信息。", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ScenicOperationDynamicInfo scenicOperationDynamicInfo) {
        return toAjax(operationInfoService.insertOperationInfo(scenicOperationDynamicInfo));
    }

    /**
     * 修改运营动态信息。
     */
    @PreAuthorize("@ss.hasPermi('system:info:edit')")
    @Log(title = "运营动态信息。", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ScenicOperationDynamicInfo scenicOperationDynamicInfo) {
        return toAjax(operationInfoService.updateOperationInfo(scenicOperationDynamicInfo));
    }

    /**
     * 删除运营动态信息。
     */
    @PreAuthorize("@ss.hasPermi('system:info:remove')")
    @Log(title = "运营动态信息。", businessType = BusinessType.DELETE)
    @DeleteMapping("/{operationInfoIds}")
    public AjaxResult remove(@PathVariable Long[] operationInfoIds) {
        return toAjax(operationInfoService.deleteOperationInfoByOperationInfoIds(operationInfoIds));
    }
}
